// 登录用户仓库数据
import {login,verifyLogin,logout} from "../services/userServices";
// 异步函数延时器
// const timer = (ms) => new Promise(resolve => setTimeout(resolve,ms))

export default {
    namespaced: true, // 开始命名空间
    state: {
        data: null, // 当前登录的用户
        isLoading: false
    },

    mutations: {
        setIsLoading(state, payload) {
            state.isLoading = payload
        },
        setData(state, payload) {
            state.data = payload;
        }
    },

    actions: {
        async login(context,payload) {
            context.commit('setIsLoading',true)
            let resp = await login(payload);
            let result = 0;
            if (resp.code === 1){
                // 登录成功
                result = 1;
                context.commit('setData', resp.data)
            }
            context.commit('setIsLoading',false)
            return result;
        },

        async verifyLogin(context){
            context.commit('setIsLoading',true)
            let resp = await verifyLogin();
            if (resp.code === 1){
                // 验证成功
                context.commit('setData', resp.data)
            }
            context.commit('setIsLoading',false)
        },
        logout(context){
            logout();
            context.commit('setData',null);
        }
    }
}
